class BaseController {
  constructor(service) {
    this.service = service;
  }

  static setResponse(code, msg, other) {
    return {
      code,
      message: msg,
      success: code === 200 ? true : false,
      data: null,
      ...other,
    };
  }

  async getList(ctx) {
    const result = await this.service.getList(ctx.request.query, ctx);
    ctx.body = {
      code: 200,
      data: result,
      message: "获取列表成功",
      success: true,
    };
  }

  async getPage(ctx) {
    const result = await this.service.getPage(ctx.request.query, ctx);
    ctx.body = {
      code: 200,
      data: result.list,
      total: result.total,
      message: "获取分页列表成功",
      success: true,
    };
  }

  async add(ctx) {
    const result = await this.service.add(ctx.request.body, ctx);
    ctx.body = {
      code: 200,
      data: result,
      message: "添加成功",
      success: true,
    };
  }

  async update(ctx) {
    const _body = ctx.request.body;
    let _id = ctx.params.id;
    if (ctx.request.body.id) {
      _id = ctx.request.body.id;
    }
    if (ctx.request.query.id) {
      _id = ctx.request.query.id;
    }
    _body.id = _id;

    const result = await this.service.update(_body, ctx);
    ctx.body = {
      code: 200,
      data: result,
      message: "更新成功",
      success: true,
    };
  }

  async remove(ctx) {
    let _id = ctx.params.id;
    if (ctx.request.body.id) {
      _id = ctx.request.body.id;
    }
    if (ctx.request.query.id) {
      _id = ctx.request.query.id;
    }

    const result = await this.service.remove(_id, ctx);
    ctx.body = {
      code: 200,
      data: result,
      message: "删除成功",
      success: true,
    };
  }

  async softDelete(ctx) {
    let _id = ctx.params.id;
    if (ctx.request.body.id) {
      _id = ctx.request.body.id;
    }
    if (ctx.request.query.id) {
      _id = ctx.request.query.id;
    }

    const result = await this.service.softDelete(_id, ctx);
    ctx.body = {
      code: 200,
      data: result,
      message: "删除成功",
      success: true,
    };
  }

  async removes(ctx) {
    const _ids = ctx.request.body.ids;
    const _ids2 = ctx.request.query.ids;

    const _tgIds = [];
    if (_ids) {
      if (typeof _ids === "string") {
        _tgIds.push(..._ids.split(","));
      } else if (Object.prototype.toString.call(_ids) === "[object Array]") {
        _tgIds.push(..._ids);
      }
    }
    if (_ids2) {
      if (typeof _ids2 === "string") {
        _tgIds.push(..._ids2.split(","));
      } else if (Object.prototype.toString.call(_ids2) === "[object Array]") {
        _tgIds.push(..._ids2);
      }
    }

    const result = await this.service.removes(_tgIds, ctx);
    ctx.body = {
      code: 200,
      data: result,
      message: "批量删除成功",
      success: true,
    };
  }

  async softDeletes(ctx) {
    const _ids = ctx.request.body.ids;
    const _ids2 = ctx.request.query.ids;
    const _tgIds = [];
    if (_ids) {
      if (typeof _ids === "string") {
        _tgIds.push(..._ids.split(","));
      } else if (Object.prototype.toString.call(_ids) === "[object Array]") {
        _tgIds.push(..._ids);
      }
    }
    if (_ids2) {
      if (typeof _ids2 === "string") {
        _tgIds.push(..._ids2.split(","));
      } else if (Object.prototype.toString.call(_ids2) === "[object Array]") {
        _tgIds.push(..._ids2);
      }
    }
    const result = await this.service.softDeletes(_tgIds, ctx);
    ctx.body = {
      code: 200,
      data: result,
      message: "批量删除成功",
      success: true,
    };
  }

  async getById(ctx) {
    let _id = ctx.params.id;
    if (ctx.request.body.id) {
      _id = ctx.request.body.id;
    }
    if (ctx.request.query.id) {
      _id = ctx.request.query.id;
    }

    const result = await this.service.getById(_id, ctx);
    ctx.body = {
      code: 200,
      data: result,
      message: "获取成功",
      success: true,
    };
  }

  async getByIds(ctx) {
    const _ids = ctx.request.body.ids;
    const _ids2 = ctx.request.query.ids;
    const _tgIds = [];
    if (_ids) {
      if (typeof _ids === "string") {
        _tgIds.push(..._ids.split(","));
      } else if (Object.prototype.toString.call(_ids) === "[object Array]") {
        _tgIds.push(..._ids);
      }
    }
    if (_ids2) {
      if (typeof _ids2 === "string") {
        _tgIds.push(..._ids2.split(","));
      } else if (Object.prototype.toString.call(_ids2) === "[object Array]") {
        _tgIds.push(..._ids2);
      }
    }

    const result = await this.service.getByIds(_tgIds, ctx);
    ctx.body = {
      code: 200,
      data: result,
      message: "获取成功",
      success: true,
    };
  }
}

module.exports = BaseController;
